#include "CAnimation.h"



CAnimation::CAnimation()
{

}


CAnimation::~CAnimation()
{

}



//----------------------------------//
// Name : CreateAnimation           //
// Desc : Create new animation info //
//----------------------------------//
void CAnimation::CreateAnimation(USHORT ID, ULONG FrameStartIndex, ULONG FrameEndIndex, ULONG FrameLoopIndex , float ActionSpeed)
{
	m_UID				= ID;
	m_nFrameStartIndex	= FrameStartIndex;
	m_nFrameEndIndex	= FrameEndIndex;
	m_nFrameLoopIndex	= FrameLoopIndex;
	m_fActionSpeed		= ActionSpeed;
	m_fLocalTime		= 0.0f;

	m_nCurrFrameIndex	= m_nFrameStartIndex;
}




//----------------------------//
// Name : UpdateAnimation     //
// Desc : Update an animation //
//----------------------------//
void CAnimation::UpdateAnimation(float deltaTime, RECT *scrRect , int width , int height , int NumFrameInRow)
{
	m_fLocalTime += deltaTime;
	if( m_fLocalTime >= m_fActionSpeed )
	{
		NextFrame();

		m_fLocalTime -= m_fActionSpeed;

		*scrRect = UpdateSourceRect( width , height , NumFrameInRow );
	}
}



//----------------------------------------------//
// Name : SetActionSpeed                        //
// Desc : Set an action speed of this animation //
//----------------------------------------------//
void CAnimation::SetActionSpeed(float NewSpeed)
{
	m_fActionSpeed = NewSpeed;
}


//-----------------------------------------------//
// Name : SetStartFrameIndex                     //
// Desc : Set the start index for this animation //
//-----------------------------------------------//
void CAnimation::SetStartFrameIndex(ULONG StartIndex)
{
	m_nFrameStartIndex = StartIndex;
}



//---------------------------------------------//
// Name : SetEndFrameIndex                     //
// Desc : Set the end index for this animation //
//---------------------------------------------//
void CAnimation::SetEndFrameIndex(ULONG EndIndex)
{
	m_nFrameEndIndex = EndIndex;
}



//-------------------------------------------------//
// Name : SetCurrentFrameIndex                     //
// Desc : Set the current index for this animation //
//-------------------------------------------------//
void CAnimation::SetCurrentFrameIndex(ULONG CurrentIndex)
{
	if( CurrentIndex < m_nFrameStartIndex || CurrentIndex > m_nFrameEndIndex )
		return;

	m_nCurrFrameIndex = CurrentIndex;
}


//----------------------------------------------//
// Name : SetFrameLoopIndex                     //
// Desc : Set the loop index for this animation //
//----------------------------------------------//
void CAnimation::SetFrameLoopIndex(ULONG LoopIndex)
{
	if( LoopIndex < m_nFrameStartIndex || LoopIndex > m_nFrameEndIndex )
		return;

	m_nFrameLoopIndex = LoopIndex;
}


//-------------------------------------------------------//
// Name : GetActionSpeed                                 //
// Desc : Get the current action speed of this animation //
//-------------------------------------------------------//
float CAnimation::GetActionSpeed()
{
	return m_fActionSpeed;
}



//------------------------------------------------------//
// Name : GetCurrentFrameIndex                          //
// Desc : Get the current frame index of this animation // 
//------------------------------------------------------//
ULONG CAnimation::GetCurrentFrameIndex()
{
	return m_nCurrFrameIndex;
}


//------------------------------------------------------//
// Name : GetStartFrameIndex                            //
// Desc : Get the start frame index of this animation   //
//------------------------------------------------------//
ULONG CAnimation::GetStartFrameIndex()
{
	return m_nFrameStartIndex;
}


//--------------------------------------------------//
// Name : GetEndFrameIndex                          //
// Desc : Get the end frame index of this animation //
//--------------------------------------------------//
ULONG CAnimation::GetEndFrameIndex()
{
	return m_nFrameEndIndex;
}




//---------------------------------------------//
// Name : GetFrameLoopIndex                    //
// Desc : Get the loop index of this animation //
//---------------------------------------------//
ULONG CAnimation::GetFrameLoopIndex()
{
	return m_nFrameLoopIndex;
}


//------------------------------------------------------//
// Name : UpdateSourceRect                              //
// Desc : Updating the source rect of current animation // 
//------------------------------------------------------//
RECT CAnimation::UpdateSourceRect(int width , int height , int NumFrameInRow)
{
	RECT scrRect;
	

	scrRect.left		= ( m_nCurrFrameIndex % NumFrameInRow ) * width ;
	scrRect.right		= scrRect.left + width ;
	scrRect.top			= ( m_nCurrFrameIndex / NumFrameInRow ) * height ;
	scrRect.bottom		= scrRect.top + height ;

	return scrRect;
}


//-----------------------------------//
// Name : NextFrame                  //
// Desc : Move to next frame		 //
//-----------------------------------//
void CAnimation::NextFrame()
{
	m_nCurrFrameIndex++;
	if( m_nCurrFrameIndex > m_nFrameEndIndex )
		m_nCurrFrameIndex = m_nFrameLoopIndex;
}



//-----------------------------------//
// Name : PrevFrame                  //
// Desc : Move to previous frame	 //
//-----------------------------------//
void CAnimation::PrevFrame()
{
	m_nCurrFrameIndex--;
	if( m_nCurrFrameIndex < m_nFrameStartIndex )
		m_nCurrFrameIndex = m_nFrameEndIndex;
}